<!--
  This file is a part of the open-eBackup project.
  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
  If a copy of the MPL was not distributed with this file, You can obtain one at
  http://mozilla.org/MPL/2.0/.
  
  Copyright (c) [2024] Huawei Technologies Co.,Ltd.
  
  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  -->

<p class="aui-text-help-sm aui-gutter-column-md">
  {{'protection_oracle_restore_title_label' | i18n:[rowCopy?.name || rowCopy?.resource_name, oldVersion]}}
</p>
<lv-form [formGroup]="formGroup" class="formGroup" [lvLabelColon]='false'>
  <!-- 普通恢复：目标存在在线的同名数据库会导致恢复失败 -->
  <lv-form-item *ngIf="[_restoreV2Type.CommonRestore,_restoreV2Type.InstanceRestore].includes(restoreType) && !isDrill">
    <lv-form-label lvRequired>
      {{'protection_restore_to_label' | i18n}}
      <i lv-icon="aui-icon-help" lv-tooltip="{{'protection_oracle_recovery_desc_label' | i18n}}"
        class="configform-constraint" lvColorState='true'></i>
    </lv-form-label>
    <lv-form-control>
      <lv-radio-group formControlName="restoreTo" [lvGroupName]="'restoreToGroup'">
        <lv-group [lvGutter]="'20px'">
          <lv-radio [lv-tooltip]="restoreToNewLocationOnly ? ('protection_origin_restore_disabled_label'| i18n):''"
            [lvDisabled]="restoreToNewLocationOnly" [lvValue]="restoreLocationType.ORIGIN">
            {{'common_restore_to_origin_location_label' | i18n}}
            <i lv-icon="aui-icon-help" lv-tooltip="{{'protection_restore_tip_label' | i18n}}"
              lvColorState='true' class="origin-help"></i>
          </lv-radio>
          <lv-radio [lvValue]="restoreLocationType.NEW" [lvDisabled]="disableNewLocation">
            {{'common_restore_to_new_location_label' | i18n}}
          </lv-radio>
        </lv-group>
      </lv-radio-group>
    </lv-form-control>
  </lv-form-item>
  <!-- 表级恢复：只支持原位置-->
  <lv-form-item *ngIf="restoreType === _restoreType.FileRestore">
    <lv-form-label lvRequired>
      {{'protection_restore_to_label' | i18n}}
      <!-- 表级恢复需要目标主机在线 提示内容待修改 -->
      <i lv-icon="aui-icon-help" lv-tooltip="{{'protection_oracle_recovery_desc_label' | i18n}}"
        class="configform-constraint" lvColorState='true'></i>
    </lv-form-label>
    <lv-form-control>
      <lv-radio-group formControlName="restoreTo" [lvGroupName]="'restoreToGroup'">
        <lv-radio [lvValue]="restoreLocationType.ORIGIN">
          {{'common_restore_to_origin_location_label' | i18n}}
          <i lv-icon="aui-icon-help" lv-tooltip="{{'protection_restore_tip_label' | i18n}}"
            lvColorState='true'></i>
        </lv-radio>
      </lv-radio-group>
    </lv-form-control>
  </lv-form-item>
  <ng-container *ngIf='restoreLocationType.ORIGIN === formGroup.value.restoreTo && rowCopy'>
    <lv-form-item>
      <lv-form-label>
        {{'common_location_label' | i18n}}
      </lv-form-label>
      <lv-form-control>
        <input lv-input type='text' formControlName='name' />
        <ng-container *ngIf="formGroup.get('name').status !== 'VALID' && originalError">
          <lv-group lvGutter="4px" class="lv-form-control-error-default error-margin">
            <div class="original-new-tip">
              <i lv-icon="lv-icon-status-failed"></i>
              <span>{{originalError}}</span>
            </div>
          </lv-group>
        </ng-container>
      </lv-form-control>
    </lv-form-item>
  </ng-container>
  <ng-container *ngIf='restoreLocationType.NEW === formGroup.value.restoreTo'>
    <aui-select-tag class="select-tag" [formGroup]="formGroup" (updateTable)="updateTable($event)"
      [targetKey]="'targetHost'" [isAgentTag]="true"></aui-select-tag>
    <lv-form-item>
      <lv-form-label lvRequired>
        {{(isCluster ? 'explore_target_host_cluster_label' : 'common_target_host_label') | i18n}}
      </lv-form-label>
      <lv-form-control [lvErrorTip]='targetHostErrorTip' class="target-host-error-tip">
        <lv-select [lvOptions]="targetHostOption" formControlName="targetHost" lvValueKey="value" lvShowFilter
          lvFilterKey='label' lvFilterMode='contains'>
        </lv-select>
      </lv-form-control>
    </lv-form-item>
    <!--存储快照副本下 选中集群会出现选择节点的下拉框-->
    <lv-form-item *ngIf="isSnapshotCopy && selectedHostIsCluster">
      <lv-form-item></lv-form-item>
      <lv-form-control>
        <lv-select [lvOptions]="singleNodeOpts" lvValueKey="value" formControlName="singleNode" lvShowFilter
          lvFilterMode="contains" lvFilterKey="label"></lv-select>
      </lv-form-control>
    </lv-form-item>
    <!--  指定实例，非存储层快照才需要选择  -->
    <lv-form-item *ngIf="!isSnapshotCopy && !!formGroup.value.targetHost">
      <lv-form-label></lv-form-label>
      <lv-form-control>
        <div style="width: 90%">
          <lv-datatable formArrayName="instanceConfig" lvResize lvFake>
            <thead>
              <tr>
                <th>
                  <lv-form-label lvRequired>
                    {{ 'protect_origin_instance_label' | i18n }}
                  </lv-form-label>
                </th>
                <th width="130px">{{ 'protection_node_ip_address_label' | i18n }}</th>
                <th>{{ 'protection_target_instance_label' | i18n }}</th>
              </tr>
            </thead>
            <tbody>
              <ng-container *ngFor="let item of instanceConfig.controls; let i = index">
                <tr [formGroupName]="i">
                  <td>
                    <lv-form-control style="width: 180px">
                      <lv-select [lvOptions]="originalInstanceOptions" (ngModelChange)="instanceSelectionChange($event)"
                        formControlName="selectInstance" lvValueKey="value" lvShowClear lvShowFilter lvFilterKey="label"
                        lvFilterMode="contains" style="width: 100%">
                      </lv-select>
                    </lv-form-control>
                  </td>
                  <td>
                    <span>
                      {{ item.value.endpoint | nil }}
                    </span>
                  </td>
                  <td>
                    <lv-form-control [lvErrorTip]="instanceInputErrorTip" class="database-config-writeable"
                      style="width: 200px">
                      <input lv-input [placeholder]="instanceInputLabel" formControlName="targetInstance"
                        style="width: 100%" />
                    </lv-form-control>
                  </td>
                </tr>
              </ng-container>
            </tbody>
          </lv-datatable>
        </div>
      </lv-form-control>
    </lv-form-item>
    <ng-container *ngIf="targetVersion || targetHostIPs">
      <lv-form-item>
        <lv-form-label></lv-form-label>
        <lv-form-control>
          <lv-group lvDirection='vertical'>
            <lv-group *ngIf="targetVersion">
              <span>{{'common_target_host_version_label' | i18n: []: true}}</span>
              <span>{{targetVersion | nil}}</span>
            </lv-group>
            <lv-group *ngIf="targetHostIPs">
              <span>{{'protection_specified_host_ip_label' | i18n: []: true}}</span>
              <div style="width: 400px;" lv-overflow>{{targetHostIPs | nil}}</div>
            </lv-group>
          </lv-group>
        </lv-form-control>
      </lv-form-item>
    </ng-container>
    <lv-form-item *ngIf="!isSnapshotCopy">
      <lv-form-label>
        {{'common_destination_path_label' | i18n}}
        <i lv-icon="aui-icon-help" [lv-tooltip]="destinationPathTpl" lvTooltipClassName="customer"
           class="configform-constraint" lvColorState='true'></i>
      </lv-form-label>
      <lv-form-control [lvErrorTip]='destinationPathErrorTip'>
        <lv-group>
          <input lv-input type='text' formControlName='destinationPath' />
        </lv-group>
        <div class="aui-text-help-sm restore-path-desc">
          <i lv-icon="lv-icon-status-info" lvColorState='true'></i>
          <span>
            {{'protection_oracle_restore_path_desc_label' | i18n}}
          </span>
        </div>
      </lv-form-control>
    </lv-form-item>
    <!-- 目标磁盘 -->
    <ng-container *ngIf="isSnapshotCopy">
      <lv-group lvGutter='8px' style="margin-bottom: 10px">
        <span class="custom-collapse-title-text">{{'common_target_disk_label'|i18n}}</span>
      </lv-group>
      <lv-group lvDirection="vertical" lvGutter="10px">
        <lv-alert lvType="info" [lvClosable]="false">
          {{'protection_oracle_restore_asm_disk_alert_label'|i18n}}
        </lv-alert>
        <ng-container *ngIf="showAlert">
          <lv-alert lvType="error">{{'protection_target_disk_not_enough_label' | i18n}}</lv-alert>
        </ng-container>
        <lv-group class="target-disk-title">
          <lv-pro-button-group [config]="optsConfig"></lv-pro-button-group>
          <div>
            <span
              class="disk-number">{{selectedDiskNumber}}/{{totalDisk}}</span>{{'protection_selected_target_disk_label'|i18n}}/{{'protection_disk_to_be_restore_label'|i18n}}
          </div>
        </lv-group>
      </lv-group>
      <lv-pro-table [config]="restoreDiskConfig" [data]="restoreDiskData"></lv-pro-table>
    </ng-container>
  </ng-container>
  <div class="collapse-header">
    <lv-collapse [lvMultiExpansion]="false" lvType="simple">
      <lv-collapse-panel [lvTitle]="titleTpl" [lvExpanded]="false">
        <ng-container *ngIf="isSnapshotCopy">
          <lv-form-item>
            <lv-form-label>
              {{'protection_clients_label' | i18n}}
            </lv-form-label>
            <lv-form-control>
              <lv-select [lvOptions]="proxyHostOptions" formControlName="proxyHost" lvValueKey='value' lvMode="multiple"
                lvShowFilter lvFilterMode="contains" lvFilterKey="label" [lvContentTemplate]="contentTpl">
              </lv-select>
              <ng-template #contentTpl let-item>
                <lv-group lvGutter="4px">
                  <i
                    lv-icon="{{ item.linkStatus === dataMap.resource_LinkStatus_Special.normal.value ? 'aui-host-online' : 'aui-host-offline'}}"></i>
                  <span>{{ item.label }}</span>
                  <span class="aui-text-help-sm">
                    {{ item.extendInfo.scenario === dataMap.proxyHostType.builtin.value ?
                    hostBuiltinLabel
                    : hostExternalLabel
                    }}
                  </span>
                </lv-group>
              </ng-template>
            </lv-form-control>
          </lv-form-item>
          <lv-form-item>
            <lv-form-label>
              {{ 'protection_concurrency_number_label' | i18n }}
            </lv-form-label>
            <lv-form-control [lvErrorTip]="concurrencyErrorTip">
              <input lv-input type="text" formControlName="concurrency" placeholder="1-8" />
            </lv-form-control>
          </lv-form-item>
        </ng-container>
        <ng-container *ngIf="!isSnapshotCopy">
          <lv-form-item *ngIf="restoreType !== _restoreType.FileRestore">
            <lv-form-label>{{'protection_disable_bct_label' | i18n}}</lv-form-label>
            <lv-form-control>
              <lv-switch formControlName='bctStatus'></lv-switch>
            </lv-form-control>
          </lv-form-item>
          <lv-form-item>
            <lv-form-label>{{'protection_channel_number_label' | i18n}}</lv-form-label>
            <lv-form-control>
              <lv-group lvGutter='8px' class="channel-container">
                <lv-switch formControlName='numberOfChannelOpen'></lv-switch>
                <ng-container *ngIf='formGroup.value.numberOfChannelOpen'>
                  <lv-form-control [lvErrorTip]='numberOfChannelRangeErrorTip'>
                    <input lv-input formControlName="numberOfChannels" type="text" placeholder="1~254"
                      class="number-of-channels" />
                  </lv-form-control>
                </ng-container>
              </lv-group>
            </lv-form-control>
          </lv-form-item>
        </ng-container>
        <!--存储层快照副本不支持启动数据库选项-->
        <lv-form-item *ngIf="restoreType === _restoreV2Type.CommonRestore && !isSnapshotCopy">
          <lv-form-label>{{'explore_start_database_label' | i18n}}</lv-form-label>
          <lv-form-control>
            <lv-switch formControlName='power_on'>
            </lv-switch>
          </lv-form-control>
        </lv-form-item>
        <lv-form-item *ngIf="!isDrill">
          <lv-form-label>
            {{'protection_script_label' | i18n}}
            <i lv-icon="aui-icon-help"  class="mgl-4"
              lv-tooltip="{{ 'protection_recovery_advance_params_desc2_label' | i18n }}" lvColorState='true'>
            </i>
          </lv-form-label>
          <lv-form-control>
            <lv-switch formControlName='scriptOpen'></lv-switch>
            <ng-container *ngIf='formGroup.value.scriptOpen'>
              <lv-form-item>
                <lv-form-label>
                  {{'protection_restore_pre_script_label' | i18n}}
                  <i lv-icon="aui-icon-help"
                    lv-tooltip="{{rowCopy.environment_os_type ===
                    dataMap.Os_Type.windows.value ? i18n.get('common_script_agent_windows_position_label') : i18n.get('common_script_agent_position_label')}}"
                     class="configform-constraint" lvColorState='true'></i>
                </lv-form-label>
                <lv-form-control [lvErrorTip]='scriptErrorTip'>
                  <lv-group>
                    <input lv-input type='text' formControlName='preProcessing'
                      placeholder="{{rowCopy.environment_os_type ===
                      dataMap.Os_Type.windows.value ? i18n.get('common_script_windows_placeholder_label') : i18n.get('common_script_linux_placeholder_label')}}" />
                  </lv-group>
                </lv-form-control>
              </lv-form-item>
              <lv-form-item>
                <lv-form-label>
                  {{'protection_restore_post_script_label' | i18n}}
                  <i lv-icon="aui-icon-help"
                    lv-tooltip="{{rowCopy.environment_os_type ===
                    dataMap.Os_Type.windows.value ? i18n.get('common_script_agent_windows_position_label') : i18n.get('common_script_agent_position_label')}}"
                     class="configform-constraint" lvColorState='true'></i>
                </lv-form-label>
                <lv-form-control [lvErrorTip]='scriptErrorTip'>
                  <lv-group>
                    <input lv-input type='text' formControlName='postProcessing'
                      placeholder="{{rowCopy.environment_os_type ===
                      dataMap.Os_Type.windows.value ? i18n.get('common_script_windows_placeholder_label') : i18n.get('common_script_linux_placeholder_label')}}" />
                  </lv-group>
                </lv-form-control>
              </lv-form-item>
              <lv-form-item>
                <lv-form-label>
                  {{'protection_restore_fail_script_label' | i18n}}
                  <i lv-icon="aui-icon-help"
                    lv-tooltip="{{rowCopy.environment_os_type ===
                    dataMap.Os_Type.windows.value ? i18n.get('common_script_agent_windows_position_label') : i18n.get('common_script_agent_position_label')}}"
                     class="configform-constraint" lvColorState='true'></i>
                </lv-form-label>
                <lv-form-control [lvErrorTip]='scriptErrorTip'>
                  <lv-group>
                    <input lv-input type='text' formControlName='restore_failed_script'
                      placeholder="{{rowCopy.environment_os_type ===
                      dataMap.Os_Type.windows.value ? i18n.get('common_script_windows_placeholder_label') : i18n.get('common_script_linux_placeholder_label')}}" />
                  </lv-group>
                </lv-form-control>
              </lv-form-item>
            </ng-container>
          </lv-form-control>
        </lv-form-item>
        <ng-container
          *ngIf="restoreLocationType.NEW === formGroup.value.restoreTo && ![dataMap.CopyData_generatedType.import.value].includes(rowCopy.generated_by) && !isSnapshotCopy">
          <aui-database-config [formGroup]="formGroup" [rowCopy]="rowCopy" [isDrill]="isDrill"></aui-database-config>
        </ng-container>
      </lv-collapse-panel>
    </lv-collapse>
  </div>
</lv-form>

<ng-template #titleTpl>
  <lv-group lvGutter='8px'>
    <span class="aui-h3">{{'common_advanced_label'|i18n}}</span>
  </lv-group>
</ng-template>

<ng-template #targetHostContentTpl let-item>
  <ng-container *ngIf="item.is_cluster">
    <lv-group lvGutter='5px'>
      <i lv-icon='aui-icon-cluster' [lvColorState]="true"></i>
      <div lv-overflow>{{item.label}}</div>
    </lv-group>
  </ng-container>
  <ng-container *ngIf="!item.is_cluster">
    <lv-group lvGutter='5px'>
      <i lv-icon='aui-icon-host' [lvColorState]="true"></i>
      <div lv-overflow>{{item.label}}</div>
    </lv-group>
  </ng-container>
</ng-template>


<ng-template #destinationPathTpl>
  <span innerHTML="{{'protection_oracle_restore_path_help_label' | i18n}}"></span>
</ng-template>

<ng-template #selectDiskTpl let-item>
  <lv-select [lvOptions]="item.diskOptions" lvShowFilter lvFilterKey="label" lvFilterMode="contains" lvShowClear
    [lvContentTemplate]="diskContentTpl" style="width: 100%" lvValueKey="value" [(ngModel)]="item.target_disk"
    (ngModelChange)="diskSelectionChange($event,item)">
    <ng-template #diskContentTpl let-disk>
      <lv-group lvDirection="vertical">
        <div class="disk-content-tpl">
          <span lv-overflow class="content-label">{{disk.label}}</span>
          <span class="desc-label">{{'common_free_size_label'|i18n}}:{{disk.disk_size_label}}</span>
        </div>
        <div class="desc-label">
          {{disk.asm_group | nil}}
        </div>
      </lv-group>
    </ng-template>
  </lv-select>
</ng-template>

<ng-template #clusterInstanceTpl let-item>
  <input lv-input [(ngModel)]="item.targetInstance" [placeholder]="instanceInputLabel" />
</ng-template>

<ng-template #tipTpl>
  <div>
    {{(isSnapshotCopy? 'protection_oracle_snapshot_restore_tip_label' :'protection_oracle_restore_tip_label' )|
    i18n:[(rowCopy?.display_timestamp || rowCopy?.restoreTimeStamp*1000) |
    date: 'yyyy-MM-dd HH:mm:ss']}}
  </div>
  <div *ngIf="restoreLocationType.ORIGIN === formGroup.value.restoreTo">
    {{'protection_restore_tip_label' | i18n}}
  </div>
  <div *ngIf="appUtilsService.isDistributed && rowCopy.generated_by===dataMap.CopyData_generatedType.replicate.value">
    {{'explore_distributed_restore_database_tip_label' | i18n}}</div>
  <lv-group lvGutter='8px' class="mgt">
    <span class="taget-label">{{'protection_restore_target_label' | i18n}}</span>
    <span>{{restoreTargetLabel}}</span>
  </lv-group>
</ng-template>

<ng-template #targetDiskThTpl>
  <i lv-icon="aui-icon-help" [lv-tooltip]="'protection_oracle_restore_target_disk_tips_label'|i18n"
     class="configform-constraint" lvColorState='true'></i>
</ng-template>
